<!-- 信息分类界面 -->
<script setup>
import { ref } from 'vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import {
  artGetChannelsService,
  artAddChannelService,
  artEditChannelService,
  artDelChannelService
} from '../../api/article'

const channelList = ref([]) //分类列表数据
const loading = ref(false)
const dialogVisible = ref(false) //控制添加分类弹窗
const title = ref('') // 定义变量控制标题展示
//添加分类数据模型
const categoryModel = ref({
  categoryName: '',
  categoryAlias: ''
})
//添加分类表单校验
const rules = {
  categoryName: [
    { required: true, message: '请输入分类名称', trigger: 'blur' }
  ],
  categoryAlias: [
    { required: true, message: '请输入分类别名', trigger: 'blur' }
  ]
}

// 获取分类列表数据
const getChannelList = async () => {
  loading.value = true
  const res = await artGetChannelsService()
  console.log('分类', res)
  channelList.value = res.data.data
  loading.value = false
}
getChannelList()

// 点击添加表单按钮
const add = () => {
  categoryModel.value = {}
  dialogVisible.value = true //显示弹窗
  title.value = '添加分类'
}
// 弹窗--添加表单确定按钮
const addCategory = async () => {
  const result = await artAddChannelService(categoryModel.value)
  console.log('添加分类', result)
  ElMessage.success(result.message ? result.message : '添加成功')
  dialogVisible.value = false
  // 渲染列表
  getChannelList()
}

// 点击编辑分类图标
const editCategory = (row) => {
  dialogVisible.value = true //显示弹窗，触发编辑分类确定按钮或取消按钮
  title.value = '编辑分类'

  // 数据拷贝
  categoryModel.value.categoryName = row.categoryName
  categoryModel.value.categoryAlias = row.categoryAlias
  // 扩展id属性
  categoryModel.value.id = row.id
}

// 弹窗--编辑分类确定按钮
const updateCategory = async () => {
  const result = await artEditChannelService(categoryModel.value)
  ElMessage.success(result.message ? result.message : '修改成功')
  dialogVisible.value = false
  // 渲染列表
  getChannelList()
  categoryModel.value = {}
}

// 点击删除分类图标
const delCategory = async (row) => {
  await ElMessageBox.confirm('你确认要删除该分类么', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
    .then(async () => {
      await artDelChannelService(row.id)
      ElMessage.success('删除成功')
      getChannelList()
    })
    .catch(() => {
      ElMessage.info('用户取消了删除')
    })
}
</script>

<template>
  <!-- 调用子组件  title #extra 默认插槽-->
  <page-container title="信息分类">
    <template #extra>
      <el-button @click="add">添加分类</el-button>
    </template>

    <!-- 默认插槽 列表 -->
    <el-table v-loading="loading" :data="channelList" style="width: 100%">
      <el-table-column type="index" label="序号" width="200"></el-table-column>
      <el-table-column prop="categoryName" label="分类名称"></el-table-column>
      <el-table-column prop="categoryAlias" label="分类别名"></el-table-column>
      <el-table-column label="操作" width="150">
        <template #default="{ row }">
          <el-button
            :icon="Edit"
            circle
            plain
            type="primary"
            @click="editCategory(row)"
          ></el-button>
          <el-button
            :icon="Delete"
            circle
            plain
            type="danger"
            @click="delCategory(row)"
          ></el-button>
        </template>
      </el-table-column>

      <template #empty>
        <el-empty description="没有数据"></el-empty>
      </template>
    </el-table>

    <!-- 添加/编辑分类弹窗 -->
    <el-dialog v-model="dialogVisible" :title="title" width="30%">
      <el-form
        :model="categoryModel"
        :rules="rules"
        label-width="100px"
        style="padding-right: 30px"
      >
        <el-form-item label="分类名称" prop="categoryName">
          <el-input
            v-model="categoryModel.categoryName"
            minlength="1"
            maxlength="10"
          ></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="categoryAlias">
          <el-input
            v-model="categoryModel.categoryAlias"
            minlength="1"
            maxlength="15"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button
            type="primary"
            @click="title == '添加分类' ? addCategory() : updateCategory()"
          >
            确认
          </el-button>
        </span>
      </template>
    </el-dialog>
  </page-container>
</template>

<style lang="scss" scoped></style>
